package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import dao.OrderDetailDao;
import dao.RowMapper;
import entity.Food;
import entity.FoodSize;
import entity.FoodType;
import entity.OrderDetail;
import entity.Rate;

public class OrderDetailDaoImpl implements OrderDetailDao {
	private JDBCTemplate jdbc = new JDBCTemplate();
	
	@Override
	public void addOrderDetail(OrderDetail detail) {
		String sql = "INSERT INTO orderDetail(rateId,foodCount,orderParentId,currentPrice) "+ 
					 " VALUES(?,?,?,?);";
		jdbc.CUDObject(sql, detail.getRate().getRateId(),detail.getFoodCount(),detail.getOrderParentId(),detail.getCurrentPrice());
	}
	
	private class RowMapperImpl implements RowMapper<OrderDetail>{

		@Override
		public OrderDetail rowMapper(ResultSet rs) throws SQLException {
			OrderDetail detail = new OrderDetail();
			detail.setOrderDetailId(rs.getInt("orderDetailId"));
			detail.setCurrentPrice(rs.getDouble("currentPrice"));
			detail.setFoodCount(rs.getInt("foodCount"));
			detail.setOrderParentId(rs.getString("orderParentId"));
			
			Rate rate = new Rate();
			
			Food food = new Food();
			food.setfId(rs.getInt("foodId"));
			food.setDescription(rs.getString("description"));
			food.setFoodName(rs.getString("foodName"));
			food.setFoodprice(rs.getDouble("foodprice"));
			food.setImageFile(rs.getString("imageFile"));
			
			FoodType foodType = new FoodType();
			foodType.setFtId(rs.getInt("typeId"));
			foodType.setFoodTypeName(rs.getString("foodTypeName"));
			
			food.setFoodType(foodType);
			
			FoodSize foodSize = new FoodSize();
			foodSize.setFsId(rs.getInt("foodSizeId"));
			foodSize.setFoodSizeName(rs.getString("foodSizeName"));
			
			rate.setFood(food);
			rate.setFoodSize(foodSize);
			rate.setRateId(rs.getInt("rateId"));
			rate.setRatio(rs.getDouble("ratio"));
			
			detail.setRate(rate);
			return detail;
		}
		
	}
	@Override
	public List<OrderDetail> queryAllOrderDetail() {
		String sql = "SELECT"+ 
					" orderDetail.*,ratio,"+
					" foodId,foodName,foodprice,imageFile,description,"+
					" food.`foodTypeId` AS typeId,foodTypeName,"+
					" foodSizeId,foodSizeName"+
					" FROM orderDetail"+
					" INNER JOIN rate ON orderDetail.`rateId`=rate.`rateId`"+
					" INNER JOIN food ON rate.`foodId`=food.`fId`"+
					" INNER JOIN foodType ON food.`foodTypeId`=foodType.`ftId`"+
					" INNER JOIN foodSize ON rate.`foodSizeId` = foodSize.`fsId`";
		return jdbc.queryAll(sql, new RowMapperImpl());
	}

	@Override
	public OrderDetail queryOrderDetailByOrderDetailId(int orderDetailId) {
		String sql = "SELECT"+ 
				" orderDetail.*,ratio,"+
				" foodId,foodName,foodprice,imageFile,description,"+
				" food.`foodTypeId` AS typeId,foodTypeName,"+
				" foodSizeId,foodSizeName"+
				" FROM orderDetail"+
				" INNER JOIN rate ON orderDetail.`rateId`=rate.`rateId`"+
				" INNER JOIN food ON rate.`foodId`=food.`fId`"+
				" INNER JOIN foodType ON food.`foodTypeId`=foodType.`ftId`"+
				" INNER JOIN foodSize ON rate.`foodSizeId` = foodSize.`fsId`"+
				" WHERE orderDetailId=?";
		return jdbc.queryOne(sql, new RowMapperImpl(), orderDetailId);
	}

	@Override
	public List<OrderDetail> queryOrderDetailByOrderParentId(String orderParentId) {
		String sql = "SELECT"+ 
				" orderDetail.*,ratio,"+
				" foodId,foodName,foodprice,imageFile,description,"+
				" food.`foodTypeId` AS typeId,foodTypeName,"+
				" foodSizeId,foodSizeName"+
				" FROM orderDetail"+
				" INNER JOIN rate ON orderDetail.`rateId`=rate.`rateId`"+
				" INNER JOIN food ON rate.`foodId`=food.`fId`"+
				" INNER JOIN foodType ON food.`foodTypeId`=foodType.`ftId`"+
				" INNER JOIN foodSize ON rate.`foodSizeId` = foodSize.`fsId`"+
				" WHERE orderParentId=?";
		return jdbc.queryAll(sql, new RowMapperImpl(), orderParentId);
	}

}
